<template>
  <div>
    <el-card class="box-card">
      <el-page-header content="商品回收站"></el-page-header>
      <el-button type="primary" icon="el-icon-search" @click="onSubmit">条件筛选</el-button>
      <el-form :model="searchMap" label-width="120px">
        <el-form-item label="输入搜索：">
          <el-input v-model="searchMap.name" placeholder="商品名称/商品货号"></el-input>
        </el-form-item>
        <el-form-item label="商品分类：">
          <el-select v-model="searchMap.category1Id" placeholder="请选择商品分类">
            <el-option label="不选择" value></el-option>
            <el-option
              v-for="item in categories"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品品牌：">
          <el-select v-model="searchMap.brandId" placeholder="请选择品牌">
            <el-option label="不选择" value></el-option>
            <el-option v-for="item in brands" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="编号" width="140"></el-table-column>
        <el-table-column prop="image" label="商品图片" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.skuImage" width="160px" height="80px" />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="category1Id" label="商品分类" width="180"></el-table-column>
        <el-table-column prop="price" label="价格" width="130"></el-table-column>
        <el-table-column prop="sn" label="货号" width="80"></el-table-column>
        <el-table-column prop="address" label="* 操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="updateIsdelete(scope.$index, scope.row)"
            >还原商品</el-button>
            <el-button type="primary" size="mini" @click="deleted(scope.$index, scope.row)">删除商品</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="block">
        <span class="demonstration">完整功能</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizeList"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchMap: {
        status: 0
      },
      tableData: [],
      categories: [],
      brands: [],
      currentPage: 1, // 页号
      pageSize: 10, // 页大小
      pageSizeList: [5, 10, 20, 30],
      total: 0
    }
  },
  methods: {
    // 还原商品
    updateIsdelete (index, rows) {
      this.$confirm('是否把回收站商品还原到本地, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rows.isDelete = '0'
        this.$http.post(`goods/spu/updateIsDelete`, rows)
        this.tableData.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除商品
    deleted (index, rows) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rows.isDelete = '3'
        this.$http.post(`goods/spu/updateIsDelete`, rows)
        this.tableData.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async initDatas () {
      const { data: res } = await this.$http.post(`goods/spu/initCommodityRecyclingStation/${this.currentPage}/${this.pageSize}`, this.searchMap)
      if (res.data.spuPojoList != null) {
        this.tableData = res.data.spuPojoList
      }
      this.brands = res.data.brands
      this.categories = res.data.categories
      this.total = res.data.total[0]
    },
    async handleSizeChange (val) {
      this.pageSize = val;
      this.initDatas();
    },
    async handleCurrentChange (val) {
      this.currentPage = val;
      this.initDatas();
    },
    onSubmit () {
      this.initDatas()
    }
  },
  created () {
    this.initDatas()
  }
}
</script>

<style lang="less" scoped>
</style>
